/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.devmng.web;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.devmng.entity.DevchkAnswer;
import com.thinkgem.jeesite.modules.devmng.entity.DevchkItem;
import com.thinkgem.jeesite.modules.devmng.entity.DevchkItemAnswer;
import com.thinkgem.jeesite.modules.devmng.service.DevchkAnswerService;
import com.thinkgem.jeesite.modules.devmng.service.DevchkItemAnswerService;
import com.thinkgem.jeesite.modules.devmng.service.DevchkItemService;
import com.thinkgem.jeesite.modules.devmng.vo.DevchkItemVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 检修项Controller
 *
 * @author panda
 * @version 2017-12-02
 */
@Controller
@RequestMapping(value = "${adminPath}/devmng/devchkItem")
public class DevchkItemController extends BaseController {

    @Autowired
    private DevchkItemService devchkItemService;

    @Autowired
    private DevchkAnswerService devchkAnswerService;

    @Autowired
    private DevchkItemAnswerService devchkItemAnswerService;

    @ModelAttribute
    public DevchkItem get(@RequestParam(required = false) String id) {
        DevchkItem entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = devchkItemService.get(id);
        }
        if (entity == null) {
            entity = new DevchkItem();
        }
        return entity;
    }

    @RequiresPermissions("devmng:devchkItem:view")
    @RequestMapping(value = {"treeIndex"})
    public String treeIndex() {
        return "modules/devmng/devchkItemTreeList";
    }

    @RequiresPermissions("devmng:devchkItem:view")
    @RequestMapping(value = {"list", ""})
    public String list(DevchkItem devchkItem, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<DevchkItem> page = devchkItemService.findPage(new Page<DevchkItem>(request, response), devchkItem);
        model.addAttribute("page", page);
        return "modules/devmng/devchkItemList";
    }

    @RequiresPermissions("devmng:devchkItem:view")
    @RequestMapping(value = "form")
    public String form(DevchkItem devchkItem, Model model) {
        model.addAttribute("devchkItem", devchkItem);
//        List<DevchkAnswer> allDevchkAnswers = devchkAnswerService.findList(new DevchkAnswer());
//        model.addAttribute("allDevchkAnswers", allDevchkAnswers);
        List<DevchkItemAnswer> devchkItemAnswerList = devchkItemAnswerService.listByDevchkItemId(devchkItem.getId());
        if (DevchkItem.Meature.noneMeature.name().equals(devchkItem.getMeature())) {
            // do nothing
        } else if (DevchkItem.Meature.singleMeature.name().equals(devchkItem.getMeature())) {
            DevchkItemAnswer devchkItemAnswer = getDevchkItemAnswer(devchkItemAnswerList, "2"); // 单个测量值
            if (devchkItemAnswer != null) {
                devchkItem.setSingleMeatureContentDesc(devchkItemAnswer.getContentDesc());
                devchkItem.setSingleMeatureUnitCode(devchkItemAnswer.getUnitCode());
                devchkItem.setMinValue(devchkItemAnswer.getMinValue());
                devchkItem.setMaxValue(devchkItemAnswer.getMaxValue());
            }
        } else if (DevchkItem.Meature.multiMeature.name().equals(devchkItem.getMeature())) {
            setMultiMeatureInfo(devchkItem, devchkItemAnswerList);
        } else if (StringUtils.isEmpty(devchkItem.getMeature()) && hasDevchkAnswer(devchkItem.getDevchkAnswers(), "2")) {
            // 历史数据兼容，单个测量值
            devchkItem.setMeature(DevchkItem.Meature.singleMeature.name());
            DevchkItemAnswer devchkItemAnswer = getDevchkItemAnswer(devchkItemAnswerList, "2"); // 单个测量值
            if (devchkItemAnswer != null) {
                devchkItem.setSingleMeatureContentDesc(devchkItemAnswer.getContentDesc());
                devchkItem.setSingleMeatureUnitCode(devchkItemAnswer.getUnitCode());
                devchkItem.setMinValue(devchkItemAnswer.getMinValue());
                devchkItem.setMaxValue(devchkItemAnswer.getMaxValue());
            }
        }

        model.addAttribute("hasDevChkAnswer1", hasDevchkAnswer(devchkItem.getDevchkAnswers(), "1")); // 是否有正常不正常选项
        model.addAttribute("hasDevChkAnswer4", hasDevchkAnswer(devchkItem.getDevchkAnswers(), "4")); // 是否有图片选项
        return "modules/devmng/devchkItemForm";
    }

    private void setMultiMeatureInfo(DevchkItem devchkItem, List<DevchkItemAnswer> devchkItemAnswerList) {
        DevchkItemAnswer devchkItemAnswer = getDevchkItemAnswer(devchkItemAnswerList, "5"); // 多个测量值
        if (devchkItemAnswer != null) {
            devchkItem.setMultiMeatureContentDesc1(devchkItemAnswer.getContentDesc());
            devchkItem.setMultiMeatureUnitCode1(devchkItemAnswer.getUnitCode());
            devchkItem.setMinValue1(devchkItemAnswer.getMinValue());
            devchkItem.setMaxValue1(devchkItemAnswer.getMaxValue());
        }
        devchkItemAnswer = getDevchkItemAnswer(devchkItemAnswerList, "6"); // 多个测量值
        if (devchkItemAnswer != null) {
            devchkItem.setMultiMeatureContentDesc2(devchkItemAnswer.getContentDesc());
            devchkItem.setMultiMeatureUnitCode2(devchkItemAnswer.getUnitCode());
            devchkItem.setMinValue2(devchkItemAnswer.getMinValue());
            devchkItem.setMaxValue2(devchkItemAnswer.getMaxValue());
        }
        devchkItemAnswer = getDevchkItemAnswer(devchkItemAnswerList, "7"); // 多个测量值
        if (devchkItemAnswer != null) {
            devchkItem.setMultiMeatureContentDesc3(devchkItemAnswer.getContentDesc());
            devchkItem.setMultiMeatureUnitCode3(devchkItemAnswer.getUnitCode());
            devchkItem.setMinValue3(devchkItemAnswer.getMinValue());
            devchkItem.setMaxValue3(devchkItemAnswer.getMaxValue());
        }
        devchkItemAnswer = getDevchkItemAnswer(devchkItemAnswerList, "8"); // 多个测量值
        if (devchkItemAnswer != null) {
            devchkItem.setMultiMeatureContentDesc4(devchkItemAnswer.getContentDesc());
            devchkItem.setMultiMeatureUnitCode4(devchkItemAnswer.getUnitCode());
            devchkItem.setMinValue4(devchkItemAnswer.getMinValue());
            devchkItem.setMaxValue4(devchkItemAnswer.getMaxValue());
        }
        devchkItemAnswer = getDevchkItemAnswer(devchkItemAnswerList, "9"); // 多个测量值
        if (devchkItemAnswer != null) {
            devchkItem.setMultiMeatureContentDesc5(devchkItemAnswer.getContentDesc());
            devchkItem.setMultiMeatureUnitCode5(devchkItemAnswer.getUnitCode());
            devchkItem.setMinValue5(devchkItemAnswer.getMinValue());
            devchkItem.setMaxValue5(devchkItemAnswer.getMaxValue());
        }
        devchkItemAnswer = getDevchkItemAnswer(devchkItemAnswerList, "10"); // 多个测量值
        if (devchkItemAnswer != null) {
            devchkItem.setMultiMeatureContentDesc6(devchkItemAnswer.getContentDesc());
            devchkItem.setMultiMeatureUnitCode6(devchkItemAnswer.getUnitCode());
            devchkItem.setMinValue6(devchkItemAnswer.getMinValue());
            devchkItem.setMaxValue6(devchkItemAnswer.getMaxValue());
        }

        devchkItemAnswer = getDevchkItemAnswer(devchkItemAnswerList, "11"); // 多个测量值
        if (devchkItemAnswer != null) {
            devchkItem.setMultiMeatureContentDesc7(devchkItemAnswer.getContentDesc());
            devchkItem.setMultiMeatureUnitCode7(devchkItemAnswer.getUnitCode());
            devchkItem.setMinValue7(devchkItemAnswer.getMinValue());
            devchkItem.setMaxValue7(devchkItemAnswer.getMaxValue());
        }

        devchkItemAnswer = getDevchkItemAnswer(devchkItemAnswerList, "12"); // 多个测量值
        if (devchkItemAnswer != null) {
            devchkItem.setMultiMeatureContentDesc8(devchkItemAnswer.getContentDesc());
            devchkItem.setMultiMeatureUnitCode8(devchkItemAnswer.getUnitCode());
            devchkItem.setMinValue8(devchkItemAnswer.getMinValue());
            devchkItem.setMaxValue8(devchkItemAnswer.getMaxValue());
        }

        devchkItemAnswer = getDevchkItemAnswer(devchkItemAnswerList, "13"); // 多个测量值
        if (devchkItemAnswer != null) {
            devchkItem.setMultiMeatureContentDesc9(devchkItemAnswer.getContentDesc());
            devchkItem.setMultiMeatureUnitCode9(devchkItemAnswer.getUnitCode());
            devchkItem.setMinValue9(devchkItemAnswer.getMinValue());
            devchkItem.setMaxValue9(devchkItemAnswer.getMaxValue());
        }

        devchkItemAnswer = getDevchkItemAnswer(devchkItemAnswerList, "14"); // 多个测量值
        if (devchkItemAnswer != null) {
            devchkItem.setMultiMeatureContentDesc10(devchkItemAnswer.getContentDesc());
            devchkItem.setMultiMeatureUnitCode10(devchkItemAnswer.getUnitCode());
            devchkItem.setMinValue10(devchkItemAnswer.getMinValue());
            devchkItem.setMaxValue10(devchkItemAnswer.getMaxValue());
        }
    }

    private DevchkItemAnswer getDevchkItemAnswer(List<DevchkItemAnswer> devchkItemAnswerList, String devchkAnswerId) {
        DevchkItemAnswer result = null;
        for (DevchkItemAnswer devchkItemAnswer : devchkItemAnswerList) {
            if (devchkItemAnswer.getDevchkAnswerId().equals(devchkAnswerId)) {
                result = devchkItemAnswer;
                break;
            }
        }
        return result;
    }

    private boolean hasDevchkAnswer(List<DevchkAnswer> allDevchkAnswers, String id) {
        boolean flag = false;
        for (DevchkAnswer devchkAnswer : allDevchkAnswers) {
            if (devchkAnswer.getId().equals(id)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    @RequiresPermissions("devmng:devchkItem:view")
    @RequestMapping(value = "copy")
    public String copy(DevchkItem devchkItem, Model model) {
        model.addAttribute("devchkItem", devchkItem);
        List<DevchkAnswer> allDevchkAnswers = devchkAnswerService.findList(new DevchkAnswer());
        model.addAttribute("allDevchkAnswers", allDevchkAnswers);
        devchkItem.setId(null);
        return "modules/devmng/devchkItemForm";
    }

    @RequiresPermissions("devmng:devchkItem:edit")
    @RequestMapping(value = "save")
    public String save(DevchkItem devchkItem, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, devchkItem)) {
            return form(devchkItem, model);
        }

        if (!validateModel(devchkItem, model)) {
            return form(devchkItem, model);
        }

        devchkItemService.save(devchkItem);
        addMessage(redirectAttributes, "保存检修项成功");
        return "redirect:" + Global.getAdminPath() + "/devmng/devchkItem/?repage&deviceMain.id=" + devchkItem.getDeviceMain().getId()
                + "&deviceMain.deviceName=" + devchkItem.getDeviceMain().getDeviceName();
    }

    private boolean validateModel(DevchkItem devchkItem, Model model) {
        List<String> list = Lists.newArrayList();
        list.add(0, "数据验证失败：");
        if (DevchkItem.Meature.noneMeature.name().equals(devchkItem.getMeature())) {
            return true;
        } else if (DevchkItem.Meature.singleMeature.name().equals(devchkItem.getMeature())) {
            if (StringUtils.isEmpty(devchkItem.getSingleMeatureContentDesc())
                    || StringUtils.isEmpty(devchkItem.getSingleMeatureUnitCode())) {
                list.add("请输入内容描述与单位");
                addMessage(model, list.toArray(new String[]{}));
                return false;
            }
        } else if (DevchkItem.Meature.multiMeature.name().equals(devchkItem.getMeature())) {
            boolean c1 = StringUtils.isNotEmpty(devchkItem.getMultiMeatureContentDesc1())
                    && StringUtils.isNotEmpty(devchkItem.getMultiMeatureUnitCode1());
            boolean c2 = StringUtils.isNotEmpty(devchkItem.getMultiMeatureContentDesc2())
                    && StringUtils.isNotEmpty(devchkItem.getMultiMeatureUnitCode2());
            boolean c3 = StringUtils.isNotEmpty(devchkItem.getMultiMeatureContentDesc3())
                    && StringUtils.isNotEmpty(devchkItem.getMultiMeatureUnitCode3());
            boolean c4 = StringUtils.isNotEmpty(devchkItem.getMultiMeatureContentDesc4())
                    && StringUtils.isNotEmpty(devchkItem.getMultiMeatureUnitCode4());
            boolean c5 = StringUtils.isNotEmpty(devchkItem.getMultiMeatureContentDesc5())
                    && StringUtils.isNotEmpty(devchkItem.getMultiMeatureUnitCode5());
            boolean c6 = StringUtils.isNotEmpty(devchkItem.getMultiMeatureContentDesc6())
                    && StringUtils.isNotEmpty(devchkItem.getMultiMeatureUnitCode6());
            if (!(c1 || c2 || c3 || c4 || c5 || c6)) {
                list.add("请至少输入一个内容描述与单位");
                addMessage(model, list.toArray(new String[]{}));
                return false;
            }
        }
        return true;
    }


    @RequiresPermissions("devmng:devchkItem:edit")
    @RequestMapping(value = "delete")
    public String delete(DevchkItem devchkItem, RedirectAttributes redirectAttributes) {
        devchkItemService.delete(devchkItem);
        addMessage(redirectAttributes, "删除检修项成功");
        return "redirect:" + Global.getAdminPath() + "/devmng/devchkItem/?repage";
    }

    @ResponseBody
    @RequestMapping(value = "/viewResult", method = RequestMethod.GET)
    public List<DevchkItemVo> viewResult(@RequestParam("taskMainId") String taskMainId,
                                         @RequestParam("deviceMainId") String deviceMainId) {
        return devchkItemService.viewResult(taskMainId, deviceMainId);
    }

}